<div class="layuimini-main">

    <div class="layui-form layuimini-form" lay-filter="edit-user">
        <!--        隐藏域id-->
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label required">用户名</label>
            <div class="layui-input-block">
                <input type="text" name="userName" lay-verify="required" lay-reqtext="用户名不能为空" placeholder="请输入用户名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">联系方式</label>
            <div class="layui-input-block">
                <input type="phone" name="phone" lay-verify="required" lay-reqtext="联系方式不能为空" placeholder="请输入联系方式" value="" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">用户类型</label>
            <div class="layui-input-block">
                <select name="type" lay-verify="required" lay-reqtext="用户类型不能为空">
                    <option value="">请选择</option>
                    <option value="0">管理员</option>
                    <option value="1">宿管员</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认编辑</button>
            </div>
        </div>
    </div>
</div>

<script>
    layui.use(['form','axios','layer','jquery'], function () {

        var form = layui.form,
            $ = layui.jquery,
            layer = layui.layer,
            axios = layui.axios;
        /**
         * 初始化表单，要加上，不然刷新部分组件可能会不加载
         */
        form.render();

        // 当前弹出层，防止ID被覆盖
        var parentIndex = layer.index;

        //监听提交
        form.on('submit(saveBtn)', function (data) {
            var DISABLED = 'layui-btn-disabled';

            $(':button').addClass(DISABLED); // 添加样式

            $(':button').attr('disabled', 'disabled');  // 添加属性
            axios({
                method:"post"
                ,url:'/user/edit'
                ,data:data.field
            }).then(res=>{
                if(res.code==0){
                  layer.msg(res.message, {
                        icon: 6,
                        time: 2000 //2秒关闭（如果不配置，默认是3秒）
                    }, function(){
                    layer.close(parentIndex);
                    layui.table.reload('user-table-id');
                    $(':button').removeClass(DISABLED);
                    $(':button').removeAttr('disabled');    
                  })    
                }else{
                  layer.msg(res.message,{
                      icon: 5,
                      time: 2000, //2秒关闭（如果不配置，默认是3秒）
                      anim:6
                  });
                  $(':button').removeClass(DISABLED);
                  $(':button').removeAttr('disabled');    
                }
            }).catch(res=>{
              layer.msg("请求错误",{
                  icon: 5,
                  time: 2000, //2秒关闭（如果不配置，默认是3秒）
                  anim:6
              });
              $(':button').removeClass(DISABLED);
              $(':button').removeAttr('disabled');
            });
            return false;
        });
    });
</script>